class Solution(object):
    def minimumDeletions(self, s):
        n = len(s)
        prefix = [0] * n
        prefix[0] = int(s[0] == 'a')
        for i in range(1, n):
            prefix[i] = prefix[i - 1] + int(s[i] == 'a')
        min_len = prefix[-1]
        for i in range(n):
            min_len = min(min_len, i + 1 - prefix[i] + prefix[-1] - prefix[i])
        return min_len